/**
 */
"use strict";

var Router = require('koa-router');
var config = require('../../config/config.js');
var router = new Router({
    prefix: config.baseUrl
});
module.exports = router.routes();
console.log('router:' + config.baseUrl);

/**
 * all v2 router
 */
router.post('/**', function*(next) {
    //console.log('global router post');
    var inParams;
    if (typeof this.request.body === 'string') {
        try {
            inParams = JSON.parse(this.request.body);
        } catch (e) {
            // TODO
            this.body = 'error : input must be json';
            this.status = 400;
            return;
        }
    } else {
        inParams = this.request.body;
    }

    this.request.inParams = inParams;
    yield next;
});

router.put('/**', function*(next) {
    //console.log('global router post');
    var inParams;
    if (typeof this.request.body === 'string') {
        try {
            inParams = JSON.parse(this.request.body);
        } catch (e) {
            // TODO
            this.status = 400;
            this.body = 'error : input must be json';
            return;
        }
    } else {
        inParams = this.request.body;
    }

    this.request.inParams = inParams;
    yield next;
});

router.get('/**', function*(next) {
    //console.log('global router get');
    this.request.inParams = this.request.query;
    yield next;
});


